package com.interview.javabase.algorithm;

import java.util.Arrays;

/**
 * @description: 递归创建链表
 * @author: zhangshuguang@vichain.com
 * @date: 2019/07/24 14:32
 */
public class CreateLinked {

    public static Node createLink(Node[] arr){
        Node headNode = arr[0];

        if (arr.length == 1){
            return arr[arr.length-1];
        }
        Node firstNode = createLink(Arrays.copyOfRange(arr, 1, arr.length));
        headNode.setNext(firstNode);
        return headNode;
    }

    public static void main(String[] args) {
        Node[] arr = {new Node().setValue("100"),new Node().setValue("200"),new Node().setValue("300")};
        Node link = createLink(arr);
        System.out.println(link);
    }
}

class Node{
    private String value;

    private Node next = null;

    public String getValue() {
        return value;
    }

    public Node setValue(String value) {
        this.value = value;
        return this;
    }

    public Node getNext() {
        return next;
    }

    public Node setNext(Node next) {
        this.next = next;
        return this;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Node{");
        sb.append("value='").append(value).append('\'');
        sb.append(", next=").append(next);
        sb.append('}');
        return sb.toString();
    }
}
